package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @auther hewenhui
 * @see <a href="hetp://www.codingmaster.com> java </a>
 */
public class UserDao {
    // 增：添加新用户呗
    public static void insert(User user){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1，和数据库建立连接
            connection=DBUtil.getConnection();
            //2，构造失去了语句
            String sql="insert into user values(?,?,?);";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,user.getUserId());
            preparedStatement.setString(2, user.getUsername());
            preparedStatement.setString(3,user.getPassword());
            //3，执行sql语句
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4，关闭资源
            DBUtil.close(connection,preparedStatement,null);
        }
    }
    // 删:根据userId来进行删除
    public static void delete(int userId){
        // 删除和增加以及修改好像都不要使用ResultSet对象，因为没有返回的内容
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1,连接数据库
            connection=DBUtil.getConnection();
            //2，构造失去了 语句
            String sql = "delete from user where userId = ?;";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,userId);
            //3，执行
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4,关闭资源
            DBUtil.close(connection,preparedStatement,null);
        }
    }
    // 改:只能修改密码！
    public static void updatePassword(User user){
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            //1,连接数据库
            connection=DBUtil.getConnection();
            //2,构造sql语句
            String sql = "update user set password='?';";
            statement = connection.prepareStatement(sql);
            statement.setString(1,user.getPassword());
            //3,执行
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //4,关闭连接
            DBUtil.close(connection,statement,null);
        }
    }
    // 查：根据id查找
    public static User selectById(int userId){
        Connection connection=null;
        PreparedStatement Statement=null;
        ResultSet resultSet=null;
        try {
            //1,连接数据库
            connection=DBUtil.getConnection();
            //2，构造查询语句
            String sql = "select * from user where userId = ?;";
            Statement=connection.prepareStatement(sql);
            Statement.setInt(1,userId);
            //3,执行
            resultSet = Statement.executeQuery();
            //执行完了以后，我们需要根据查询结果把返回的Blog对象给构造出来
            //resultSet.next() 这个就是迭代遍历用的相当于foreach
            User user = new User();
            if(resultSet.next()){
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                //注意参数的类型，类名一样不代表同一个类，可能是来自不同的包的类
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,Statement,resultSet);
        }
        return null;
    }
    // 查：根据用户名查找
    public static User selectByName(String username){
        Connection connection=null;
        PreparedStatement Statement=null;
        ResultSet resultSet=null;
        User user = new User();
        try {
            //1,连接数据库
            connection=DBUtil.getConnection();
            //2，构造查询语句
            String sql = "select * from user where username = ?";
            Statement=connection.prepareStatement(sql);
            Statement.setString(1,username);
            //3,执行
            resultSet = Statement.executeQuery();
            //执行完了以后，我们需要根据查询结果把返回的Blog对象给构造出来
            //resultSet.next() 这个就是迭代遍历用的相当于foreach

            if(resultSet.next()){
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                //注意参数的类型，类名一样不代表同一个类，可能是来自不同的包的类

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,Statement,resultSet);
        }
        return user;
    }
}
